﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;
using Gfw.BLL.Basic.User;
using Gfw.Web.Models.Users;
using L.Basic.Authority.Context;

namespace Gfw.Web.Areas.Admin.Controllers
{
    [LAuthorize]
    public class DefaultController : Controller
    {


        #region 后台首页

     
        public ActionResult Index()
        {
            return View();
        }

        #endregion

        #region 登录

        public ActionResult Login()
        {
            return View();
        }

        [AllowAnonymous]
        [HttpPost]
        public ActionResult Login(UserLoginModel model, string returnUrl)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    if (Session["ValidateCode"].ToString() != model.VerifyCode)
                        throw new Exception("验证码不正确。");
                    SystemUserAuthentication sa = new SystemUserAuthentication();
                    Context context;
                    if (
                        sa.Login(model.UserName, EncryptAlgorithmHelper.Encrypt(model.Password, model.UserName), out context)
                        )
                    {
                        L.Manage.Authentication.Context = context;
                        FormsAuthentication.SetAuthCookie(model.UserName, model.RememberMe);
                        if (Url.IsLocalUrl(returnUrl))
                        {
                            return Redirect(returnUrl);
                        }
                        else
                        {
                            return RedirectToAction("Index", "Default");
                        }
                    }

                    throw new Exception("用户名或者密码错误或者无权限");

                }
            }
            catch (Exception exp)
            {
                ModelState.AddModelError("", exp.Message);
            }
            return View(model);
        }

        #endregion



    }
}
